package com.kgc.warehouse.bussness.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kgc.warehouse.bussness.entity.Goods;
import com.kgc.warehouse.bussness.entity.Provider;
import com.kgc.warehouse.bussness.service.GoodsService;
import com.kgc.warehouse.bussness.service.ProviderService;
import com.kgc.warehouse.bussness.vo.GoodsVo;
import com.kgc.warehouse.system.common.AppFileUtils;
import com.kgc.warehouse.system.common.CodeMsg;
import com.kgc.warehouse.system.common.Constast;
import com.kgc.warehouse.system.common.ResponseResult;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("goods")
public class GoodsController {

    @Resource
    private GoodsService goodsService;

    @Resource
    private ProviderService providerService;

    @RequestMapping("loadAllGoodsForSelect")
    public ResponseResult loadAllProviderForSelect() {
        LambdaQueryWrapper<Goods> providerLambdaQueryWrapper = new LambdaQueryWrapper<>();
        providerLambdaQueryWrapper.eq(Goods::getAvailable, 1);
        List<Goods> goods = goodsService.list(providerLambdaQueryWrapper);
        List<Goods> collect = goods.stream().map(good -> {
            Provider provider = providerService.getById(good.getProviderid());
            good.setProvidername(provider.getProvidername());
            return good;
        }).collect(Collectors.toList());
        return new ResponseResult(collect, null, CodeMsg.SUCCESS);
    }

    @RequestMapping("loadGoodsByProviderId")
    public ResponseResult loadGoodsByProviderId(Integer providerid) {
        LambdaQueryWrapper<Goods> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(providerid!=null, Goods::getProviderid, providerid);
        queryWrapper.eq(Goods::getAvailable, 1);
        List<Goods> goodsList = goodsService.list(queryWrapper);
        List<Goods> collect = goodsList.stream().map(goods -> {
            Provider provider = providerService.getById(goods.getProviderid());
            if (provider != null) {
                goods.setProvidername(provider.getProvidername());
            }
            return goods;
        }).collect(Collectors.toList());
        return new ResponseResult(collect, null, CodeMsg.SUCCESS);
    }

    @RequestMapping("loadAllGoods")
    public ResponseResult loadAllGoods(GoodsVo goodsVo) {
        IPage<Goods> page = new Page<Goods>(goodsVo.getPage(), goodsVo.getLimit());
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<Goods>();
        queryWrapper.like(StringUtils.isNotBlank(goodsVo.getGoodsname()), "goodsname", goodsVo.getGoodsname());
        queryWrapper.like(StringUtils.isNotBlank(goodsVo.getProductcode()), "productcode", goodsVo.getProductcode());
        goodsService.page(page, queryWrapper);
        List<Goods> records = page.getRecords();
        for (Goods goods : records) {
            Provider provider = providerService.getById(goods.getProviderid());
            if (null!=provider){
                goods.setProvidername(provider.getProvidername());
            }
        }
        return new ResponseResult(page.getRecords(), page.getTotal(), CodeMsg.SUCCESS);
    }

    @RequestMapping("addGoods")
    public ResponseResult addGoods(GoodsVo goodsVo) {
        try {
            System.out.println("====================================");
            System.out.println(goodsVo.getGoodsimg());
            if (goodsVo.getGoodsimg() != null && goodsVo.getGoodsimg().endsWith("_temp")) {
                String newName = AppFileUtils.renameFile(goodsVo.getGoodsimg());
                goodsVo.setGoodsimg(newName);
            }
            goodsService.save(goodsVo);
            return new ResponseResult(CodeMsg.ADD_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(CodeMsg.ADD_FAIL);
        }
    }

    @RequestMapping("updateGoods")
    public ResponseResult updateGoods(GoodsVo goodsVo) {
        try {
            //商品图片不是默认图片
            if (!(goodsVo.getGoodsimg() != null && goodsVo.getGoodsimg().equals(Constast.DEFAULT_IMG_GOODS))) {

                if (goodsVo.getGoodsimg().endsWith("_temp")) {
                    String newName = AppFileUtils.renameFile(goodsVo.getGoodsimg());
                    goodsVo.setGoodsimg(newName);
                    //删除原先的图片
                    String oldPath = goodsService.getById(goodsVo.getId()).getGoodsimg();
                    AppFileUtils.removeFileByPath(oldPath);
                }
            }
            goodsService.updateById(goodsVo);
            return new ResponseResult(CodeMsg.UPDATE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(CodeMsg.UPDATE_FAIL);
        }
    }

    @RequestMapping("deleteGoods")
    public ResponseResult deleteGoods(Integer id, String goodsimg) {
        try {
            //删除商品的图片
           // AppFileUtils.removeFileByPath(goodsimg);
//            goodsService.removeById(id);
            goodsService.deleteGoodsById(id);
            return new ResponseResult(CodeMsg.DELETE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(CodeMsg.DELETE_FAIL);
        }
    }

}
